package com.yjq.programmer.controller.common;

import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.utils.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-08 10:02
 */
@RequestMapping("/music")
@RestController("MusicController")
public class MusicController {

    @Autowired
    private ResourceLoader resourceLoader;

    @Value("${yjq.upload.music.path}")
    private String uploadMusicPath; //音乐保存位置


    private static final Logger logger = LoggerFactory.getLogger(MusicController.class);

    /**
     * 系统统一的音乐获取方法
     * @param filename
     * @return
     */
    @RequestMapping(value="/view")
    public ResponseEntity<?> viewMusic(@RequestParam(name="filename",required=true) String filename){
        Resource resource = resourceLoader.getResource("file:" + uploadMusicPath + filename);
        try {
            return ResponseEntity.ok(resource);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 自定义上传音乐处理
     * @param music
     * @return
     */
    @PostMapping(value="/upload")
    public ResponseDTO<String> uploadMusic(MultipartFile music){
        if(music == null){
            return ResponseDTO.errorByMsg(CodeMsg.MUSIC_EMPTY);
        }
        //检查上传音乐文件大小 不能超过30MB
        if(music.getSize() > 30*1024*1024) {
            return ResponseDTO.errorByMsg(CodeMsg.MUSIC_SURPASS_MAX_SIZE);
        }
        //获取文件后缀
        String suffix = music.getOriginalFilename().substring(music.getOriginalFilename().lastIndexOf(".")+1, music.getOriginalFilename().length());
        if(!CommonUtil.isMusic(suffix)){
            return ResponseDTO.errorByMsg(CodeMsg.MUSIC_FORMAT_NOT_CORRECT);
        }
        String savePath = uploadMusicPath + CommonUtil.getFormatterDate(new Date(), "yyyyMMdd") + "\\";
        File savePathFile = new File(savePath);
        if(!savePathFile.exists()){
            //若不存在改目录，则创建目录
            savePathFile.mkdir();
        }
        String filename = new Date().getTime()+"."+suffix;
        logger.info("保存音乐的路径:{}",savePath + filename);
        try {
            //将文件保存至指定目录
            music.transferTo(new File(savePath + filename));
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.errorByMsg(CodeMsg.SAVE_FILE_EXCEPTION);
        }
        String filepath = CommonUtil.getFormatterDate(new Date(), "yyyyMMdd") + "/" + filename;
        return ResponseDTO.successByMsg(filepath, "音乐上传成功！");
    }
}
